package com.mmd.sharding.service;

import com.mmd.sharding.persistence.domain.Address;
import com.mmd.sharding.persistence.domain.Ceshi;
import com.mmd.sharding.persistence.domain.OrderItem;
import com.mmd.sharding.persistence.domain.TbOrder;
import com.mmd.sharding.persistence.repository.AddressRepository;
import com.mmd.sharding.persistence.repository.CeshiRepository;
import com.mmd.sharding.persistence.repository.OrderItemRepository;
import com.mmd.sharding.persistence.repository.TbOrderRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xch
 * 2023/4/20 13:29
 */
@Service
public class DemoService {

    @Resource
    private TbOrderRepository tbOrderRepository;

    @Resource
    private OrderItemRepository orderItemRepository;

    @Resource
    private AddressRepository addressRepository;

    @Resource
    private CeshiRepository ceshiRepository;

    public void orderDemo() {
//        for (int i = 10; i < 20; i++) {
//            TbOrder tbOrder = new TbOrder();
//            tbOrder.setOrderId((long)i);
//            tbOrder.setUserId(1L);
//            tbOrderRepository.save(tbOrder);
//        }

//        List<TbOrder> allByDemo = tbOrderRepository.findAllByDemo("1");
//        System.out.println(allByDemo);

        List<Long> orderIds = new ArrayList<>();
        orderIds.add(1L);
        List<TbOrder> allByDemo2 = tbOrderRepository.findAllByDemo2(orderIds);
        System.out.println(allByDemo2);
    }

    public void orderItemDemo() {
        for (int i = 0; i < 5; i++) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderItemId((long)i);
            orderItem.setOrderId((long)i);
            orderItem.setUserId(1L);
            orderItemRepository.save(orderItem);
        }
        for (int i = 5; i < 10; i++) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderItemId((long)i);
            orderItem.setOrderId((long)i);
            orderItem.setUserId(2L);
            orderItemRepository.save(orderItem);
        }
    }

    public void caseWhenValid01() {
        List<TbOrder> tbOrders = tbOrderRepository.caseWhenValid01();
        System.out.println(tbOrders);
    }

    public void caseWhenValid02() {
        List<TbOrder> tbOrders = tbOrderRepository.caseWhenValid02();
        System.out.println(tbOrders);
    }

    /**
     * 广播表，保存都会在两个数据库的表中保存
     */
    public void addressDemo() {
        Address address = new Address();
        address.setId(1L);
        address.setCity("测试");
        addressRepository.save(address);
    }


    /**
     * 广播表的查询，两个库中的两个表都有数据，但是查询出来，只会出来一个数据;
     */
    public void addressQuery() {
        Address address = addressRepository.findById(1L).orElseThrow(() -> new RuntimeException("插叙失败"));
        System.out.println(address);

        List<Long> ss = new ArrayList<>(1);
        ss.add(1L);
        List<Address> allById = addressRepository.findAllById(ss);
        //一条数据
        System.out.println(allById);
    }

    /**
     * 1.  当ceshi表在 datasource1 的时候  数据总是插入到 datasource 1
     *
     * 2. 当datasource1 中没有表，而datasouce2 中有表，就会插入到datasource 1
     *
     * 3. 当两个数据源都有表的时候，还是会向datasource1 中插入;
     */
    public void ceshiDemo() {
        Ceshi ceshi = new Ceshi();
        ceshi.setCeshiId(1L);
        ceshiRepository.save(ceshi);
    }

    /**
     * 查询依然使用此逻辑
     * 1.  当ceshi表在 datasource1 的时候  数据总是插入到 datasource 1
     *
     * 2. 当datasource1 中没有表，而datasouce2 中有表，就会插入到datasource 1
     *
     * 3. 当两个数据源都有表的时候，还是会向datasource1 中插入;
     */
    public void ceshiQueryDemo() {
        Ceshi ceshi = ceshiRepository.findById(1L).orElseThrow(() -> new RuntimeException("不存在"));
        System.out.println(ceshi);
    }



}
